/**
 * Created by Administrator on 2017/8/10.
 */
/**
 * 显示页面
 */
EUI.StudentView = EUI.extend(EUI.CustomUI, {
    renderTo: null,
    isEdit: false,
    initComponent: function () {
        EUI.Container({
            renderTo: this.renderTo,
            layout: "border",
            border: false,
            padding: 8,
            itemspace: 0,
            items: [this.initTbar(), this.initGrid()]
        });
        this.gridCmp = EUI.getCmp("gridPanel");
        this.addEvents();
    },

    initTbar: function () {
        var g = this;
        return {
            xtype: "ToolBar",
            region: "north",
            height: 40,
            padding: 0,
            style: {
                overflow: "hidden"
            },
            border: false,
            items: [{
                xtype: "Button",
                //引入图标
                iconCss:"ecmp-common-add",
                //引用static/lang下面的多语言
                title: this.lang.addText,
                selected: true,
                handler: function () {
                    g.isEdit = false;
                    g.addAndEdit();
                }
            }, '->', {
                xtype: "SearchBox",
                displayText: g.lang.searchBoxText,
                onSearch: function (value) {
                    g.gridCmp.localSearch(value);
                },
                afterClear: function () {
                    g.gridCmp.localSearch();
                }
            }]
        };
    },
    initGrid: function () {
        var g = this;
        return {
            xtype: "GridPanel",
            region: "center",
            id: "gridPanel",
            style: {
                "border": "1px solid #aaa",
                "border-radius": "3px"
            },
            gridCfg: {
                url: _ctxPath + "/student/listAll",
                // url: "../student/listAll",
                loadonce: true,
                searchConfig: {
                    searchCols: ["code", "name"]
                },
                colModel: [{
                    label: this.lang.operateText,
                    name: "operate",
                    index: "operate",
                    width: 130,
                    align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        return "<i  class='ecmp-common-edit icon-space' title='"+g.lang.modifyText+"'></i><i class='ecmp-common-delete' title='"+g.lang.deleteText+"'></i>";
                    }
                }, {
                    name: "id",
                    index: "id",
                    hidden: true
                }, {
                    label: g.lang.codeText,
                    name: "code",
                    index: "code",
                    width: 120,
                    sortable: true,
                    // align:"left"
                }, {
                    label: g.lang.nameText,
                    name: "name",
                    index: "name",
                    width: 120,
                    sortable: true,
                }],
                rowNum: 15,
                sortname: "name",
                // shrinkToFit: false
            }
        };
    },
    addEvents: function () {
        var g = this;
        $(".ecmp-common-edit").live("click", function () {
            var data = g.gridCmp.getSelectRow();
            g.isEdit = true;
            g.addAndEdit();
            g.editFormCmp.loadData(data);
        });
        $(".ecmp-common-delete").live("click", function () {
            var rowData = EUI.getCmp("gridPanel").getSelectRow();
            var infoBox = EUI.MessageBox({
                title: g.lang.deleteText,
                msg: g.lang.deleteHintMessageText,
                buttons: [{
                    title: g.lang.okText,
                    iconCss:"ecmp-common-ok",
                    selected: true,
                    handler: function () {
                        var myMask = EUI.LoadMask({
                            msg: g.lang.deleteMaskMessageText,
                        });
                        EUI.Store({
                            url: _ctxPath + "/student/delete",
                            params: {
                                id: rowData.id
                            },
                            success: function (result) {
                                myMask.hide();
                                infoBox.remove();
                                var status = {
                                    msg: result.msg,
                                    success: result.success,
                                    showTime: result.success ? 2 : 60
                                };
                                if (status.success) {
                                    g.gridCmp.refreshGrid();
                                }
                                EUI.ProcessStatus(status);
                            },
                            failure: function (re) {
                                myMask.hide();
                                infoBox.remove();
                                var status = {
                                    msg: re.msg,
                                    success: false,
                                    showTime: 6
                                };
                                EUI.ProcessStatus(status);
                            },

                        });

                    }
                }, {
                    title: g.lang.cancelText,
                    iconCss:"ecmp-common-delete",
                    handler: function () {
                        infoBox.remove();
                    }
                }]
            });
        });
    },
    addAndEdit: function () {
        var g = this;
        g.addAndEditWin = EUI.Window({
            title: g.isEdit ? g.lang.updateDemoGeneralText : g.lang.addNewDemoGeneralText,
            height: 70,
            width: 420,
            padding: 15,
            items: [{
                xtype: "FormPanel",
                id: "editForm",
                padding: 0,
                defaultConfig: {
                    xtype: "TextField",
                    labelWidth: 120,
                    width: 250
                },
                items: [{
                    name: "id",
                    hidden: true
                }, {
                    title: g.lang.codeText,
                    allowBlank: false,
                    name: "code",
                    maxlength: 20,
                    afterValid: function (valid) {
                        EUI.TextField().afterValid.call(this, valid);
                        this.dom.field.val(this.getValue().toUpperCase());
                    }
                }, {
                    title: g.lang.nameText,
                    allowBlank: false,
                    name: "name",
                    maxlength: 30
                }]
            }],
            buttons: [{
                title: g.lang.saveText,
                iconCss:"ecmp-common-save",
                selected: true,
                handler: function () {
                    g.save();
                }
            }, {
                title: g.lang.cancelText,
                iconCss:"ecmp-common-delete",
                handler: function () {
                    g.addAndEditWin.remove();
                }
            }],
        });
        g.editFormCmp = EUI.getCmp("editForm");
    },
    save: function () {
        var g = this;
        if (!g.editFormCmp.isValid()) {
            g.message(g.lang.unFilledText);
            return;
        }
        var data = g.editFormCmp.getFormValue();
        if (!g.isEdit) {
            delete data.id;
        }
        var myMask = EUI.LoadMask({
            msg: g.lang.saveMaskMessageText,
        });
        EUI.Store({
                url: _ctxPath + "/student/save",
                params: data,
                success: function (result) {
                    myMask.hide();
                    var status = {
                        msg: result.msg,
                        success: result.success,
                        showTime: result.success ? 2 : 60
                    };
                    if (status.success) {
                        g.addAndEditWin.remove();
                        g.gridCmp.refreshGrid();
                    }
                    EUI.ProcessStatus(status);
                },
                failure: function (re) {
                    myMask.hide();
                    var status = {
                        msg: re.msg,
                        success: false,
                        showTime: 6
                    };
                    g.addAndEditWin.remove();
                    EUI.ProcessStatus(status);
                }
            }
        );

    },
    message: function (msg) {
        var g = this;
        var message = EUI.MessageBox({
            border: true,
            //hintText: "提示",
            title: g.lang.hintText,
            showClose: true,
            msg: msg,
            buttons: [{
                //okText:"确定",
                title: g.lang.okText,
                iconCss:"ecmp-common-ok",
                handler: function () {
                    message.remove();
                }
            }]
        });
    }
});